package com.oscar.ehome.utils.view;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.oscar.ehome.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Viewpager适配器
 *
 * @author apple
 */
public class BasePhotoAdapter extends PagerAdapter {
    private List<Integer> pageview = new ArrayList<Integer>();
    Activity act;
    LayoutInflater mInflater;

    public BasePhotoAdapter(Activity act, List<Integer> pageview) {
        this.act = act;
        this.pageview = pageview;
        this.mInflater = LayoutInflater.from(act);
    }

    @Override
    // 获取当前窗体界面数
    public int getCount() {
        // TODO Auto-generated method stub
        if (pageview != null) {
            return pageview.size();
        }
        return 0;
    }

    @Override
    // 断是否由对象生成界面
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == arg1;
    }

    // 是从ViewGroup中移出当前View
    @Override
    public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
        if (arg2 != null) {
            ViewGroup viewPager = ((ViewGroup) arg0);
            int count = viewPager.getChildCount();
            for (int i = 0; i < count; i++) {
                View childView = viewPager.getChildAt(i);
                if (childView == arg2) {
                    viewPager.removeView(childView);
                    break;
                }
            }
        }
    }

    // 返回一个对象，这个对象表明了PagerAdapter适配器选择哪个对象放在当前的ViewPager中
    @Override
    public Object instantiateItem(ViewGroup arg0, int arg1) {
        ViewHolder holder;
        View view = mInflater.inflate(R.layout.adapter_lead, null);
        holder = new ViewHolder();
        holder.leadImagview = (ImageView) view.findViewById(R.id.lead_imagview);
        switch (arg1){
            case 0:
                holder.leadImagview.setImageResource(R.mipmap.ydy1);
                break;
            case 1:
                holder.leadImagview.setImageResource(R.mipmap.ydy4);
                break;
            case 2:
                holder.leadImagview.setImageResource(R.mipmap.ydy3);
                break;
            case 3:
                holder.leadImagview.setImageResource(R.mipmap.ydy2);
                break;
        }
        ((ViewGroup) arg0).addView(view);
        return view;
    }

    class ViewHolder {
        /**
         * 引导页图片
         */
        ImageView leadImagview;
    }
}
